package 商城进货交易记录程序设计;

import java.io.File;
import java.util.Scanner;

public class KeyWordSearch implements INoperation{
    @Override
    public void workFIle() {
        //关键字查询
        System.out.println("请输入要检索的目录");
        Scanner sc = new Scanner(System.in);
        String directory = sc.nextLine();
        System.out.println("请输入要检索的关键字");
        String KeyWord = sc.nextLine();
        //进行目录操作
        File fileDirectory = new File(directory);
        if (!fileDirectory.exists() || !fileDirectory.isDirectory()) {
            System.out.println("目录不存在");
            return;
        }
        WordSearch(fileDirectory, KeyWord);
    }
    //调用比对方法递归查找
    private static void WordSearch(File fileDirectory, String keyWord)
    {
        File[] files = fileDirectory.listFiles();//拆分路径下的目录
        if(files != null)
        {
            for(File file : files)
            {
                if(file.isDirectory())
                {
                    WordSearch(file,keyWord);//递归查找
                }
                else if(file.getName().contains(keyWord)){
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }

}
